//
//  FriendViewController.swift
//  chenChat
//
//  Created by chen on 16/2/20.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit

class FriendViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    
    var tableView: UITableView = UITableView()
    var tableSource: NSMutableArray = []
    override func viewDidLoad() {
        super.viewDidLoad()
        //
        let friend1: UserInfo = UserInfo(id: "1", name: "chen")
        let friend2: UserInfo = UserInfo(id: "2", name: "zhou")
        let friend3: UserInfo = UserInfo(id: "3", name: "xiao")
        let friend4: UserInfo = UserInfo(id: "4", name: "laji")
        tableSource.addObjectsFromArray([friend1,friend2,friend3,friend4])
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.registerClass(FriendCell.self, forCellReuseIdentifier: FriendCell.reuseIdentifier())
        let width = self.view.frame.width
        let height = self.view.frame.height
        self.tableView.frame = CGRectMake(0, 0, width, height)
        //在第一个cell上面部分，有点儿间距罢了
//        let headerView = UIView()
//        headerView.frame = CGRectMake(0, 0, width, 10)
//        headerView.backgroundColor = UIColor.clearColor()
//        let label: UILabel = UILabel(frame: CGRect(x: 5, y: 20, width: width - 5, height: 30))
//        label.text = "FriendList"
//        self.view.addSubview(label)
//        self.tableView.tableHeaderView = headerView
        self.tableView.separatorStyle = .None
        self.tableView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(self.tableView)
        

    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        let cell = self.tableView.dequeueReusableCellWithIdentifier(FriendCell.reuseIdentifier()) as! FriendCell
        cell.selectionStyle = .None
        cell.setFriendCell(self.tableSource.objectAtIndex(indexPath.row) as! UserInfo)
        
        return cell
        
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.row)
        let user: UserInfo = self.tableSource.objectAtIndex(indexPath.row) as! UserInfo
        let chatViewController = ChatViewController()
        chatViewController.chatFriendDidChange(user)
        self.navigationController?.pushViewController(chatViewController, animated: true)
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return tableSource.count
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return FriendCell.heightForCell()
    }


}
